<?php
require_once '../db/DBClass.php';
class ToDo{
	private static $dbh;
	private function __construct(){
			
	}

	private function __destruct(){
			
	}
	public static function db(){
		if (empty(self::$dbh)){
			global $db;
			self::$dbh=$db;
		}
		return self::$dbh;
	}
	public static function new_detail_item($task_id,$data){
		$now_time = time();
		$result = self::db()->sql_query("INSERT INTO ndtl_detail (task_id,subject,description,created_date) values ({$task_id},'{$data['subject']}','{$data['description']}',{$now_time})");
		echo ($result)?self::db()->insert_id():'false';
	}
	// Catch all DetailItem for TaskItem
	public static function detail_item($task_id){
		$details = self::db()->sql_list("SELECT * FROM ndtl_detail WHERE task_id={$task_id} and state=0");
		$result = array_values($details);
		echo (empty($result))?'false':json_encode($result);
	}
	//Catch all tag of TaskItem
	public static function query_task_tags($task_id){
		$tags = self::db()->sql_list("SELECT tags.name FROM ndtl_tagging JOIN ndtl_tag tags ON tags.id = tag_id WHERE task_id = {$task_id}");
		foreach ($tags as $tag) $result[] = $tag['name'];
		echo empty($result)?'false':implode(',',$result);
	}
	//Rename task-item
	public static function ren_task_item($task_id,$task_name){
		$result = self::db()->sql_query("UPDATE ndtl_task SET name='{$task_name}' WHERE id={$task_id}");
		echo ($result)?'true':'false';
	}
	//Delete task-item
	public static function del_task_item($task_id){
		$result = self::db()->sql_query("UPDATE ndtl_task SET flag=1 WHERE id={$task_id}");
		echo ($result)?'true':'false';
	}
	//Add task-item
	public static function new_task_item($list_id,$task_name){
		$now_time = time();	
		$result = self::db()->sql_query("INSERT INTO ndtl_task (list_id,name,created_date) values ({$list_id},'{$task_name}',{$now_time})");
		echo ($result)?self::db()->insert_id():'false';
	}
	//Resize task-item 
	public static function resize_task_item($task_id,$size){
		$result = self::db()->sql_query("UPDATE ndtl_task SET size='{$size}' WHERE id={$task_id}");
		echo ($result)?'true':'false';
	}
	//Reposition task-item  
	public static function repositon_task_item($task_id,$pos){
		$result=self::db()->sql_query("UPDATE ndtl_task SET position='{$pos}' WHERE id={$task_id}");
		echo ($result)?'true':'false';
	}
	//Catch task-item data by list-id
	public static function task_item($list_id){
		$tasks=self::db()->sql_list("SELECT * FROM ndtl_task WHERE flag=0 AND list_id={$list_id} and flag=0");
		//var_dump($lists);
		$result=array_values($tasks);
		echo (empty($result))?'fals':json_encode($result);
	}
	//Update name field of list-item
	public static function ren_list_item($list_id,$list_name){
		$result = self::db()->sql_query("UPDATE ndtl_list SET name='{$list_name}' WHERE id={$list_id}");
		echo ($result)?'true':'false';
	}
	//Update flag field of list-item to 1
	public static function del_list_item($list_id){
		$result=self::db()->sql_query("UPDATE ndtl_list SET flag=1 WHERE id={$list_id}");
		echo ($result)?'true':'false';
	}
	//Append a new list-item to DB
	public static function new_list_item($list_name){
		$now_time=time();
		$result=self::db()->sql_list("SELECT MAX(position)+1 as max_position FROM ndtl_list");
		$max_position=$result[0]['max_position'];
		$result=self::db()->sql_query("INSERT INTO ndtl_list (name,position,created_date) VALUES ('{$list_name}',{$max_position},{$now_time})");
		echo ($result)?self::db()->insert_id():'false';
	}
	//Reorder list-item position field
	public static function reorder_list_item($list_item){
		foreach ($list_item as $position => $id)
		self::db()->sql_query("UPDATE ndtl_list SET position={$position} WHERE id={$id}");
	}
	//Catch all list-item and return to front side
	public static function list_item(){
		$lists=self::db()->sql_list('SELECT * FROM ndtl_list WHERE flag=0 ORDER BY position');
		//var_dump($lists);
		$result=array_values($lists);
		echo (empty($result))?'false':json_encode($result);
	}
}
?>